<?php 
$ubigeo = '';
$edit = '';
$fecdoc = '';
$fecent = '';

$ocprov = '';
$ocmonto = '';
$ocldoc = '';
$ocref = '';
$octxt = '';
$orden = '';
$disable2 = '';
$itm_cnt = 0;
$buscar = false;
$find = false;
$combo_alm = '';
$row = '';
include('../Connections/KRAPPERCON.php');
include ("../Connections/Accesos.php");
$orden = '';
$combo = '';
include('../Query_Min/getAlmacenesProc.php');
$combo_alm = $combo;
$combo_um = '';
include('../Query_Min/getUMedidas.php');
//echo str_replace('h','xxx','hola');
//echo $combo;
if (isset($_GET['solped'])) {
$orden = $_GET['solped'];
$buscar = true;
}
if ($buscar){
	$sql_oc = "call sp_findSP(".$orden.");";
	$res = $mysqli->query($sql_oc) or die($mysqli->error.__LINE__);;
	if ($res){
		$find = true;
		while($row = $res->fetch_assoc()){
			if ($itm_cnt==0){
				$edit = '';
				$disable2 = "disabled";
			// Obtener Datos de cabecera
				$fecdoc = $row['SolPedFeD'];
				$feccre = $row['SolPedFeC'];
				$userid  = $row['UserID'];
				$fecent = $row['SolPedFeE'];
				$spprov = $row['ProveedorID'];
				$spref = $row['SolPedRef'];
				$sptxt = $row['SolPedTxt'];
				$spmonto = $row['Monto'];
				$spcldoc = $row['ClaDocID'];
				$spmoneda = $row['MoneyID'];
			}
			$itm_cnt = $itm_cnt + 1;
			$sp_posn[] = $row['SolPedItm'];
			//$oc_dcto[] = $row['DetOrdDct'];
			$sp_cods[] = $row['ArticuloID'];
			$sp_noms[] = $row['ArticuloTxt'];
			$sp_prun[] = $row['PreUM'];
			$sp_qtpe[] = $row['CtdUM'];
			$sp_qtat[] = $row['CtdCons'];
			$sp_qtfi[] = $row['CtdFinal'];
			$sp_unm[] = $row['UniMed'];
			$sp_nets[] = $row['PreNet'];
			$sp_brut[] = $row['PreBru'];
			$sp_almc[] = $row['AlmacenID'];
			//$oc_cent[] = $row['CentroID'];
			$sp_dels[] = $row['StsItm'];
		}
		$res->close();
			$mysqli->check_result();
	}
}
?>

<div>
  <input type="button" value="Nueva SolPed" onclick="nuevaSP();" />
  &nbsp;
  <input style="display:<?php echo $edit ?>" type="button" value="Editar" onclick="editarSP();" />
  &nbsp;
  <input type="button" value="Grabar" onClick="grabarSP()">&nbsp;<input type="button" value="Imprimir" onClick="imprimirSP()">&nbsp;<input type="button" value="Listado" onClick="listarSP()">&nbsp;<!--<input type="button" value="Verificar" onClick="verificarOC()">-->
&nbsp;  <input type="text" name="t_mo_nsp" id="t_mo_nsp" onkeypress="showSP(event)" />
</div>
<?php 
if ($find){
?>
<div class="subTit">
<?php 
echo "Solicitud de Pedido ".$orden." creado por ".$userid." el ".$feccre;
?>
</div>
<?php
}
?>

  <table width="100%" border="0" cellspacing="0" id="tbl_dat_sp" cellpadding="0">
    <tr>
      <td>Clase de Documento :</td>
      <td>
        <select name="cb_sp_cdoc"  class='<?php echo $disable2; ?>' id="cb_sp_cdoc">
        <?php 
		$cladoc = 'SP';
		include('../Query_Min/getClaDocs.php');
		?>
        </select>
      </td>
      <td>Numero de Solicitud :</td>
      <td><span style="font-weight:bold" id="sp_nrosp"></span></td>
    </tr>
    <tr>
      <td>Fecha Documento :</td>
      <td>
        <input name="t_fecdoc"  type="text" id="t_fecdoc" size="15" maxlength="10" class='tcal clsReq'>
      </td>
      <td>Proveedor :</td>
      <td>
        <select name="cb_proveedor" class='<?php echo $disable2; ?>' id="cb_proveedor">
        <?php 			
		include('../Query_Min/getProveedores.php');
		 ?>
        </select>
      </td>
    </tr>
    <tr>
      <td>Fecha Entrega :</td>
      <td>
        <input name="t_fecent" type="text" id="t_fecent" size="15" class="tcal clsReq" maxlength="10">      </td>
      <td>Monto Total :</td>
      <td><span id="sp_monsp"></span></td>
    </tr>
    <tr>
      <td>Moneda :</td>
      <td>
        <select name="cb_sp_mon" id="cb_sp_mon">
        <?php include ('../Query_Min/getMonedas.php'); ?>
      </select>
      </td>
      <td>Referencia :</td>
      <td>
        <input type="text" name="t_sp_ref" id="t_sp_ref" />      </td>
    </tr>
    <tr>
      <td>Almacen Destino :</td>
      <td><select onchange="setOC_Alm(this)">
          <?php echo $combo_alm;   ?>
      </select></td>
      <td>Texto :</td>
      <td><input type="text" name="t_sp_txt" id="t_sp_txt" /></td>
    </tr>
    <tr>
      <td colspan="4">
        <div id="d_sp_itm">
    <?php include ('Opers/OperItmSolp.php'); ?>
    </div><br />
    <div id="d_sp_his">    </div>      </td>
      </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>

      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>

<script type="text/javascript">

function nuevaSP(){
/*	$('#sp_nroord,#KRAPPERCON.php').html('');
	GEBI('cb_oc_cdoc').selectedIndex = 0;
	GEBI('cb_proveedor').selectedIndex = 0;		
	$('#tbl_dat_oc input[type|=text]').val('');
	editarOC();
	$('#tbl_itm_oc tbody tr').not(':last').remove();
	addPos();	*/
	loadPage('Procesos/MantSolPed.php');
}

function updItemSP(){
	var oc = $.trim($("#sp_nrosp").html());
	if (oc!=''){
	$('#d_sp_itm').loadx("Procesos/Opers/OperResSP.php",
	{
	oper : 'view',
	orden : oc
	});
	}
}

function grabarSP(){
	if(!("#tbl_dat_sp").checkInput()){
	return;
	}

	var items = "";
	var orden = $.trim($('#sp_nrosp').html());	
//	if ($.trim(orden)== ""){
	// Grabar OC	
			$('#tbl_itm_sp tbody tr').each(function(){
				var fila = $(this);
				var pos = pInt(fila.attr('title'));
				//var row = fila.find('td:first');		
				var cod = fila.find('.clsCodSp').val();
				if ($.trim(cod)!="" && cod !=null){
				if (fila.hasClass('deleted'))
				{
					cod = 'DEL';
				}
				var txt = fila.find('.clsTxtSp').html();
				var pru = pFloat(fila.find('.clsPreUM').val());
				var qty1 = pInt(fila.find('.clsQtySp').val()); // Ctd en UM
				var unm = fila.find('.clsQtyUM').val();
				var qty2 = pInt(fila.find('.clsCtdFin').html()); // Ctd Final según UM
				var dct = pFloat(fila.find('.clsDctSp').val());
				var alm = fila.find('.clsSpAlm').val();
				var sts = fila.find('.clsSpSts').val();
				items = items + pos + '|' + cod + '|' + pru+ '|' + dct + '|' + qty1 + '|' + qty2  +'|' + txt + '|' + unm + '|' + sts + '|' + alm  +"#";
				}
				else {
				if (!fila.hasClass('trLast')){
					fila.remove();
				}
				}
			});
	///-----------------
		sendData('Procesos/Opers/OperSP.php',
		{
		solped : orden,
		oper : 'upd',
		cb_sp_cdoc : GEBI('cb_sp_cdoc').value,
		t_fecent : GEBI('t_fecent').value,
		t_fecdoc : GEBI('t_fecdoc').value,
		cb_proveedor : GEBI('cb_proveedor').value,	
		t_sp_ref : GEBI('t_sp_ref').value,
		t_sp_txt : GEBI('t_sp_txt').value,
		cb_sp_mon : GEBI('cb_sp_mon').value,
		items : items
		});
	
}

function imprimirOC(){
var oc = $.trim($('#sp_nrosp').html());
	if (oc!=''){
		window.open('Prints/printOrden.php?numord='+oc);
	}
	else {
	showError('Debe Cargar un Documento de Compra');
	}
}

function addPosSP(){
var newrow = $('#tbl_itm_sp tbody .trLast').clone().removeClass('trLast').css('display','');
newrow.find('input[type|=text]').val('');
$('#tbl_itm_sp tbody .trLast').before(newrow);
}

function enterSP_Cod(obj,fn){
//if (e.keyCode == 13){
	if ($.trim(obj.val()) == ''){
	obj.parent().next().find('span').html('');
	return;
	}
	var fila = obj.parent().parent();
	obj.txtLoad();
	sendData('Query_Min/getDatArticulo.php',{t_codart : obj.val() , field : 'venta'},function(){
		 var mobj  = eval('(' + $('#d_result').html() + ')');
		 fila.find('.clsTxtSp').html(mobj.desc);
		 fila.find('.clsPreUM').val(mobj.precio);
		 fila.find('.clsQtyUM').val(mobj.um);
		 //fila.find('.clsDctOC').val(myRound(mobj.dcto1));
		 obj.val(mobj.code);
		 obj.attr('title',mobj.code);
		 obj.etxtLoad();
		 setTimeout(fn,0);
	});
	//}
}

function listarSP(){
	loadPage('Consultas/LstSolPed.php');
}

function delPos_SP(obj){
	var fila = $(obj).parent().parent();
		var pos = pInt(fila.attr('title'));
		var rows = $('#tbl_itm_sp tbody tr').length;
	if (rows > 1){
		if (pos == 0){
			fila.remove();
		}
		else {
		fila.hide().addClass('deleted');
		}
		calcSP1();
	}
}

function editarSP(){
	$('#tbl_dat_sp *').enable();
}

function noEditSP(){
	$('#tbl_dat_sp *').disable();
}

function calcSP1(obj){
// recibe cantidad
//alert('');
	var monto = 0;
	var a = $('#tbl_itm_sp tbody tr').each(function(){
		var fila = $(this);
		var tcod = fila.find('.clsCodSp');
		var code = $.trim(tcod.val());
		
		var fn = function(){
			var ctd = pInt(fila.find('.clsQtySp').val());
			var pre = pFloat(fila.find('.clsPreUM').val());
			var umqty = pInt(fila.find('.clsQtyUM option:selected').attr('ctd'));
			var new_qty = ctd * umqty;
			//alert(ctd);
			//alert(umqty);
			//var dct = pFloat(fila.find('.clsDctOC').val());
			var dct = 0;
			var bruto = myRound(ctd*(pre* (1 - (dct * 0.01)) ));
			fila.find('.clsBruSp').html(bruto);
			var neto = bruto;
			fila.find('.clsNetSp').html(neto);
			fila.find('.clsCtdFin').html(new_qty);
			
			var net = 0;
			//var pvp = 0;
			
			var x  = $('#tbl_itm_sp tbody tr').each(function(){			
				var row = $(this);
				if (!row.hasClass('deleted')){
					var cod  = row.find('.clsCodSp').val();
					if ($.trim(cod)!=''){
						net = net + pFloat(row.find('.clsBruSp').html());
					}
				}
			});
			$('#sp_monsp').html(myRound(net));
			
		}
		
		if (code != tcod.attr('title')){
			enterSP_Cod(tcod,fn);
		}
		else {
			setTimeout(fn,0);
		}

	});
			
	
}

function verificarSP(){
	//calcSubt();
}

function setSP_Alm(obj){
$('.clsOCAlm').val(obj.value);
}

function introTr_SP(e){
	if (e.keyCode==13){
	calcSP1();
	}
}

function showSP(e){
	if (e.keyCode == 13){
		loadPage('Procesos/MantSolPed.php',
		{
		oper : 'view',
		solped : GEBI('t_mo_nsp').value
		});
	}
}

function verHistSP(obj){
	var pos = pInt(obj.innerHTML);
	var oc = pInt(GEBI('sp_nroord').innerHTML);
	$('#d_oc_his').loadx('Procesos/Opers/OperResOC.php',{ oper : 'hist' , orden : oc , posic : pos });
}

function calcUM_SP(row){

	var fila=$(row).parent().parent();
	cb = fila.find('.clsQtyUM');
	var umqty = pInt(cb.find('option:selected').attr('ctd'));
	
	var ctd = pInt(fila.find('.clsQtySp').val());
	var new_qty = ctd * umqty;
	fila.find('.clsCtdFin').html(new_qty);
}

<?php
if ($buscar){
	if ($find){
	?>
	$('#sp_nrosp').html('<?php echo $orden; ?>');
	GEBI('cb_sp_cdoc').value=('<?php echo $spcldoc; ?>');
	GEBI('t_fecdoc').value=('<?php echo $fecdoc ?>');
	GEBI('t_fecent').value=('<?php echo $fecent; ?>');
	$('#sp_monsp').html('<?php echo $spmonto; ?>');
	GEBI('cb_proveedor').value=('<?php echo $spprov; ?>');
	GEBI('t_sp_ref').value = '<?php echo $spref; ?>';
	GEBI('t_sp_txt').value = '<?php echo $sptxt; ?>';
	GEBI('cb_sp_mon').value = '<?php echo $spmoneda; ?>';
	//$('#tbl_itm_oc tbody').html();
	noEditSP();
	<?php 
	}
	else {
	?>
	showOk('Solicitud de Pedido <?php echo $orden ?> no existe');
	<?php 
	}
}
else {
?>
GEBI('t_fecdoc').value = '<?php echo $hoy; ?>';
GEBI('t_fecent').value = '<?php echo $hoy; ?>';
<?php 
}
?>
</script>